package com.dennytech.tac.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.dennytech.tac.base.CFFragment;
import com.dennytech.tac.net.BaseApi;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.R;

public class MoreFragment extends CFFragment implements OnClickListener {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.more_fragment, container, false);
		view.findViewById(R.id.more_feedback).setOnClickListener(this);
		view.findViewById(R.id.more_about).setOnClickListener(this);
		view.findViewById(R.id.more_share).setOnClickListener(this);
		view.findViewById(R.id.more_help).setOnClickListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setTitle(getActivity().getResources().getString(R.string.tab_name_more));
		setLeftTitleButton(0);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.more_feedback) {
			Intent email = new Intent(Intent.ACTION_SENDTO,
					Uri.parse("mailto:"));
			String[] emailReciver = new String[] { "dengjun86@gmail.com" };
			String emailSubject = getString(R.string.label_feedback_subject);
			String emailBody = getString(R.string.label_feedback_default_body);

			email.putExtra(Intent.EXTRA_EMAIL, emailReciver);
			email.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
			email.putExtra(Intent.EXTRA_TEXT, emailBody);

			try {
				startActivity(email);
			} catch (Exception e) {
				Toast.makeText(getActivity(),
						getString(R.string.label_feedback_no_email_client),
						Toast.LENGTH_SHORT).show();
			}

			Stat.event("more", "more_feedback", null, 0);

		} else if (v.getId() == R.id.more_about) {
			Intent intent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("denny://about"));
			startActivity(intent);

			Stat.event("more", "more_share", null, 0);
		} else if (v.getId() == R.id.more_share) {
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT,
					getText(R.string.label_recommend_subject));
			intent.putExtra(Intent.EXTRA_TEXT,
					getText(R.string.label_recommend_text));
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(Intent.createChooser(intent,
					getString(R.string.share)));

			Stat.event("more", "more_share", null, 0);
		} else if (v.getId() == R.id.more_help) {
			String url = BaseApi.API_DOMAIN + "help";
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(url));
			startActivity(intent);

			Stat.event("more", "more_help", null, 0);
		}
	}

}
